package com.example.certificate.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @author Hao
 *
 * Duration duration = Duration.between(now,end);
 * long days = duration.toDays(); //相差的天数
 * long hours = duration.toHours();//相差的小时数
 * long minutes = duration.toMinutes();//相差的分钟数
 * long millis = duration.toMillis();//相差毫秒数
 * long nanos = duration.toNanos();//相差的纳秒数
 */
public class ComputerTimeUtil {

    /**
     * @Description  计算两个日期之间相差的天数时间等
     * @Param [year, month, dayOfMonth]
     * @return void
     **/
    public static String calculateTimeDifferenceByPeriod(int year, Month month, int dayOfMonth) {
        String returnString = "";
        try {
            LocalDate today = LocalDate.now();
            LocalDate oldDate = LocalDate.of(year, month, dayOfMonth);
            Period p = Period.between(today,oldDate);

            if(p.getYears() == 0 && p.getMonths() == 0){
                returnString = returnString.concat(String.valueOf(p.getDays())).concat("天");
            }else if(p.getYears() != 0 && p.getMonths() == 0 && p.getDays() == 0){
                returnString = returnString.concat(String.valueOf(p.getYears())).concat("年");
            }else if(p.getYears() != 0 && p.getMonths() == 0 && p.getDays() != 0){
                if(p.getDays() > 9){
                    returnString = returnString.concat(String.valueOf(p.getYears())).concat("年").concat(String.valueOf(p.getDays())).concat("天");
                }else {
                    returnString =  returnString.concat(String.valueOf(p.getYears())).concat("年零").concat(String.valueOf(p.getDays())).concat("天");
                }
            }else if(p.getYears() != 0 && p.getMonths() != 0 && p.getDays() == 0){
                if(p.getMonths() > 9){
                    returnString = returnString.concat(String.valueOf(p.getYears())).concat("年").concat(String.valueOf(p.getMonths())).concat("月");
                }else {
                    returnString = returnString.concat(String.valueOf(p.getYears())).concat("年零").concat(String.valueOf(p.getMonths())).concat("个月");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnString;
    }


    /**
     * @Description  计算时间戳
     * @Param [year, month, dayOfMonth]
     * @return java.lang.String
     **/
    public static Integer computeTheTimestamp(String startTime,String endTime) {
        Integer returnString = 0;
        try {
            if(Duration.between(dateAnalysisTimes(startTime), dateAnalysisTimes(endTime)).toMillis() > 0){
                returnString  = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnString;
    }


    /**
     * @Description  解析日期字符串获取年份 月份 日份
     * @Param [date]
     * @return com.alibaba.fastjson.JSONObject
     **/
    public static  LocalDate dateAnalysis(String date) throws ParseException {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(date, dateTimeFormatter);
    }


    /**
     * @Description  解析日期字符串获取年份 月份 日份 时 分 秒
     * @Param [date]
     * @return com.alibaba.fastjson.JSONObject
     **/
    public static  LocalDateTime dateAnalysisTimes(String date) throws ParseException {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(date,df);
    }


    /**
     * 将格林威治时间字符串转换为yyyy-MM-dd HH:mm:ss字符串，JDK1.7以上版本支持该方法
     * @return
     */
    public static String DateString2formatString(String UTCString) {
        String returnString = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date date = sdf.parse(UTCString);
            returnString = new SimpleDateFormat("yyyy-MM-dd").format(date);
        }catch (Exception e){
            e.printStackTrace();
        }

        return returnString;
    }



    /**
     * 判断当前时间是否大于某个时间
     * @param time yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static boolean dateTimeCompare(String time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localTime=LocalDateTime.parse(time,dtf);
        return LocalDateTime.now().isAfter(localTime);
    }

    public static boolean dateCompare(String time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(time,dtf);
        return localDate.now().isAfter(localDate);
    }


    public static void main(String[] args) {

    }

}
